//
//  ImageSelectView.m
//  PhotoSelector
//
//  Created by 李李善 on 2017/9/16.
//  Copyright © 2017年 洪雯. All rights reserved.
//

#import "ImageSelectView.h"


@interface ImageSelectView()
{
  
}
@end

@implementation ImageSelectView

- (instancetype)initWithSize:(CGSize)size Inset:(UIEdgeInsets)Intset;
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = size;
    //layout.sectionInset = Intset;
//    layout.minimumLineSpacing = 0;
//    layout.minimumInteritemSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self = [super initWithFrame:CGRectZero collectionViewLayout:layout];
    if (self) {
        
        self.delegate = self;
        self.dataSource =self;
        
        self.dataImages =[NSMutableArray array];
        
        [self registerClass:[ImageSelectViewcell class] forCellWithReuseIdentifier:@"ImageSelectViewcell"];
        
    }
    return self;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (_dataImages.count==9) {
        return _dataImages.count;
    }
    return _dataImages.count+1;
//    return 9.f;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ImageSelectViewcell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ImageSelectViewcell" forIndexPath:indexPath];
    if (indexPath.item==_dataImages.count) {
        cell.imageV.hidden=YES;
        cell.backgroundColor = [UIColor yellowColor];
    }
    else
    {
        cell.imageV.hidden=NO;
        cell.backgroundColor = [UIColor clearColor];
        
            [cell update:_dataImages[indexPath.item] CollectionView:collectionView IndexPath:indexPath];
    
    }
    
    
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSInteger i;
    UIImage *image;
    if (_dataImages.count==[indexPath item]) {
        i = addImageCellType;
        image = nil;
    }
    else
    {
        i = selectCellType;
        image = _dataImages[indexPath.item];
    }
    
    if ([self.viewdelegae respondsToSelector:@selector(collectionView:didSelectItemAtIndexPath:Image:Type:)]) {
        [self.viewdelegae collectionView:self didSelectItemAtIndexPath:indexPath Image:image Type:i];
    }
}


//- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(ImageSelectViewcell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
//{
//
//}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}




-(void)updateCellForImages:(NSMutableArray *)images
{
    [_dataImages setArray:images];
    
    sleep(0.4f);
    
    [self reloadData];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end


@interface ImageSelectViewcell()
{
  
}
@end

@implementation ImageSelectViewcell

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIImageView *imageV = ({
            UIImageView *l = [UIImageView new];
            l.frame =CGRectMake(0, 0, frame.size.width,frame.size.height);
            [self.contentView addSubview:l];
            l;
            
        });
        
        UIButton *button =({
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn setImage:[UIImage imageNamed:@"close.png"] forState:UIControlStateNormal];
            [btn setFrame:CGRectMake(frame.size.width-30, -10, 30, 30)];
            [self.contentView addSubview:btn];
            btn;
        });
        
        
        _imageV = imageV;
        //self.backgroundColor = [UIColor redColor];
    }
    return self;
}


-(void)update:(id)model CollectionView:(UICollectionView *)collectionView IndexPath:(NSIndexPath *)indexPath
{
    if (_imageV&&model) {
        NSLog(@"%@--%@",NSStringFromCGRect(_imageV.frame),NSStringFromCGRect(self.frame));
        _imageV.image = model;
    }
}


/** 查看大图 */
- (void)setBigImageViewWithImage:(UIImage *)img{
    if (_BigImageView) {
        //如果大图正在显示，还原小图
        _BigImageView.frame = _imageV.frame;
        _BigImageView.image = img;
    }else{
        _BigImageView = [[UIImageView alloc] initWithImage:img];
        _BigImageView.frame = _imageV.frame;
        [self insertSubview:_BigImageView atIndex:0];
    }
    _BigImageView.contentMode = UIViewContentModeScaleToFill;
}
@end
